package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.DeviceOptions;
import com.hzhr.iot.device.constant.DeviceOptionsName;
import com.hzhr.iot.device.constant.DeviceOptionsTypeName;
import com.hzhr.iot.device.convertor.custom.CollectDeviceCustomConvertor;
import com.hzhr.iot.device.dao.CollectDeviceDao;
import com.hzhr.iot.device.entity.CollectDeviceEntity;
import com.hzhr.iot.device.entity.ext.CollectDeviceEntityExt;
import com.hzhr.iot.device.model.regulatorDevice.RegulatorDeviceGetModel;
import com.hzhr.iot.device.model.regulatorDevice.RegulatorDeviceModel;
import com.hzhr.iot.device.queryModel.custom.CollectDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.user.sdk.model.dept.DeptGetModel;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = CollectDeviceEntity.class, model = RegulatorDeviceModel.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
        @CygQuery(name = "_equal_code", query = "equal_code", type = String.class),
        @CygQuery(name = "_like_code", query = "like_code", type = String.class),
        @CygQuery(name = "_equal_categoryCode", query = "equal_categoryCode", type = String.class),
        @CygQuery(name = "_equal_deptId", query = "equal_deptId", type = Integer.class),

})
public class RegulatorDeviceService extends BaseService {
    @Autowired
    CollectDeviceCustomConvertor collectDeviceCustomConvertor;
    @Autowired
    DeviceOptions deviceOptions;
    @Autowired
    CollectDeviceDao collectDeviceDao;
    @Autowired
    User_DeptService user_deptService;

    //region javadoc注释 : get
    /**
     * 查询登录管辖范围内的调压设备 <br/><br/>
     * 步骤 : <br/>
     * 1. 查询调压设备 : <br/>
     * 根据条件参数查询采集设备(COLLECT_DEVICE)对象集合collectDeviceList_1， <br/>
     * 额外筛选规则如下： <br/>
     *           (1) collectDeviceList_1[i].manageUserId = 登录用户.userId; <br/>
     *           (2) collectDeviceList_1[i].categoryCode = REGULATOR_BOX or USER_REGULATOR_TANK or SOURCE_REGULATOR_TANK； <br/>
     * 2. 返回信息 : <br/>
     * 根据collectDeviceList_1及条件参数生成结果对象result并返回， <br/>
     * 其中，result.modelList[i]或result.model的managementName、subCompanyName属性的设置规则如下： <br/>
     *           根据result.modelList[i]或result.model的deptId属性以GLOBAL方式调用“查询部门”服务(user-service: /depts GET)， <br/>
     *           获取到对应部门模型的managementName、subCompanyName属性进行设置； <br/>
     * RegulatorDeviceModel.qrCode的设置规则如下： <br/>
     *           RegulatorDeviceModel.qrCode = {'code' : RegulatorDeviceModel.code, 'categoryCode' : RegulatorDeviceModel.categoryCode} <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_managementName : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示管理站名称;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_subCompanyName : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示分公司名称;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _is_paging : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否分页;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where regulatorDevice.id = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where regulatorDevice.code = #{_equal_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where regulatorDevice.code like #{_like_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_categoryCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where regulatorDevice.categoryCode like #{_equal_categoryCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @throws ServiceException <br/>
     * @see RegulatorDeviceGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询登录管辖范围内的调压设备
    //步骤 :
    //1. 查询调压设备 :
    //根据条件参数查询采集设备(COLLECT_DEVICE)对象集合collectDeviceList_1，
    //额外筛选规则如下：
    //          (1) collectDeviceList_1[i].manageUserId = 登录用户.userId;
    //          (2) collectDeviceList_1[i].categoryCode = REGULATOR_BOX or USER_REGULATOR_TANK or SOURCE_REGULATOR_TANK；
    //2. 返回信息 :
    //根据collectDeviceList_1及条件参数生成结果对象result并返回，
    //其中，result.modelList[i]或result.model的managementName、subCompanyName属性的设置规则如下：
    //          根据result.modelList[i]或result.model的deptId属性以GLOBAL方式调用“查询部门”服务(user-service: /depts GET)，
    //          获取到对应部门模型的managementName、subCompanyName属性进行设置；
    //RegulatorDeviceModel.qrCode的设置规则如下：
    //          RegulatorDeviceModel.qrCode = {'code' : RegulatorDeviceModel.code, 'categoryCode' : RegulatorDeviceModel.categoryCode}
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_managementName", value = "是否展示管理站名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_subCompanyName", value = "是否展示分公司名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where regulatorDevice.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_code", value = "筛选条件 : where regulatorDevice.code = #{equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_code", value = "筛选条件 : where regulatorDevice.code like #{like_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_categoryCode", value = "筛选条件 : where regulatorDevice.categoryCode = #{equal_categoryCode}",required = true, dataType = "string", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_managementName", value = "是否展示管理站名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_subCompanyName", value = "是否展示分公司名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_is_paging", value = "是否分页", defaultValue = "false",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where regulatorDevice.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_code", value = "筛选条件 : where regulatorDevice.code = #{_equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_code", value = "筛选条件 : where regulatorDevice.code like #{_like_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_categoryCode", value = "筛选条件 : where regulatorDevice.categoryCode like #{_equal_categoryCode}",required = true, dataType = "string", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/regulator-devices", method = RequestMethod.GET)
    //endregion
    public RegulatorDeviceGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        //1. 查询调压设备 :
        // 根据条件参数查询采集设备(COLLECT_DEVICE)对象集合collectDeviceList_1，
        // 额外筛选规则如下：
        // (1) collectDeviceList_1[i].manageUserId = 登录用户.userId;
        // (2) collectDeviceList_1[i].categoryCode = REGULATOR_BOX or USER_REGULATOR_TANK or SOURCE_REGULATOR_TANK；

        //生成查询模型
        UserCardModel userCard = getUserCard(header);
        CollectDeviceCustomQueryModel queryModel = collectDeviceCustomConvertor.regulatorDeviceService_query2queryModel(query);
        queryModel.setLike_resourceCode(userCard.getUserCode() + "%");
        queryModel.setEqual_isValid("1");
        String categoryCode1 = deviceOptions.getCode(DeviceOptionsTypeName.COLLECT_DEVICE_CATEGORY, DeviceOptionsName.COLLECT_DEVICE_CATEGORY.REGULATOR_BOX);
        String categoryCode2 = deviceOptions.getCode(DeviceOptionsTypeName.COLLECT_DEVICE_CATEGORY, DeviceOptionsName.COLLECT_DEVICE_CATEGORY.USER_REGULATOR_TANK);
        String categoryCode3 = deviceOptions.getCode(DeviceOptionsTypeName.COLLECT_DEVICE_CATEGORY, DeviceOptionsName.COLLECT_DEVICE_CATEGORY.SOURCE_REGULATOR_TANK);
        queryModel.setCustom1(categoryCode1);
        queryModel.setCustom2(categoryCode2);
        queryModel.setCustom3(categoryCode3);
        //查询实体
        List<CollectDeviceEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = collectDeviceDao.selectExtList2(queryModel);
        }
        //查询数量
        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = collectDeviceDao.selectCount2(queryModel);
        }

        //2. 返回信息 :
        // 根据collectDeviceList_1及条件参数生成结果对象result并返回，
        // 其中，result.modelList[i]或result.model的managementName、subCompanyName属性的设置规则如下：
        // 根据result.modelList[i]或result.model的deptId属性以GLOBAL方式调用“查询部门”服务(user-service: /depts GET)，
        // 获取到对应部门模型的managementName、subCompanyName属性进行设置；
        RegulatorDeviceGetModel result = collectDeviceCustomConvertor.extList2regulatorDeviceGetModel(extList, total, null, query);
        RegulatorDeviceModel model = result.getModel();
        if (model != null) {
            Map<String, Object> query1 = new HashMap<>();
            query1.put("show", "one");
            query1.put("_equal_id", model.getDeptId());
            Boolean _show_managementName = HzhrTypeUtil.object2boolean(query.get("_show_managementName"));
            if (_show_managementName != null && _show_managementName) {
                query1.put("_show_managementName", true);
            }
            Boolean _show_subCompanyName = HzhrTypeUtil.object2boolean(query.get("_show_subCompanyName"));
            if (_show_subCompanyName != null && _show_subCompanyName) {
                query1.put("_show_subCompanyName", true);
            }
            DeptGetModel deptGetModel = user_deptService.get(initHeader(header, AccessModeEnum.GLOBAL), query1);
            DeptModel dept = deptGetModel.getModel();
            if (dept != null) {
                model.setManagementName(dept.getManagementName());
                model.setSubCompanyName(dept.getSubCompanyName());
                Map<String, Object> map = new HashMap<>();
                String code = model.getCode();
                if (code != null){
                    map.put("code", code);
                }
                String categoryCode = model.getCategoryCode();
                if (categoryCode != null){
                    map.put("categoryCode", categoryCode);
                }

                model.setQrCode(HzhrJsonUtil.object2json(map));
            }
        }
        List<RegulatorDeviceModel> modelList = result.getModelList(); //modeList
        if (modelList != null) {
            List<Integer> deptIds = new ArrayList<>();
            for (RegulatorDeviceModel model1 :
            modelList) {
                if (model1.getDeptId() != null) {
                    deptIds.add(model1.getDeptId());
                }
            }

            Map<String, Object> query1 = new HashMap<>();
            query1.put("show", "list");
            query1.put("_in_id", deptIds);
            query1.put("_is_paging", false);
            Boolean _show_managementName = HzhrTypeUtil.object2boolean(query.get("_show_managementName"));
            if (_show_managementName != null && _show_managementName) {
                query1.put("_show_managementName", true);
            }
            Boolean _show_subCompanyName = HzhrTypeUtil.object2boolean(query.get("_show_subCompanyName"));
            if (_show_subCompanyName != null && _show_subCompanyName) {
                query1.put("_show_subCompanyName", true);
            }

            DeptGetModel deptGetModel = user_deptService.get(initHeader(header, AccessModeEnum.GLOBAL), query1);
            List<DeptModel> depts = deptGetModel.getModelList();

            for (RegulatorDeviceModel model2:
            modelList) {
                Integer deptId  = model2.getDeptId();
                if (deptId != null) {
                    for (DeptModel deptModel :
                            depts) {
                        if (deptId.equals(deptModel.getId())) {
                            model2.setManagementName(deptModel.getManagementName());
                            model2.setSubCompanyName(deptModel.getSubCompanyName());

                            Map<String, Object> map = new HashMap<>();
                            String code = model2.getCode();
                            if (code != null){
                                map.put("code", code);
                            }
                            String categoryCode = model2.getCategoryCode();
                            if (categoryCode != null) {
                                map.put("categoryCode", categoryCode);
                            }
                            model2.setQrCode(HzhrJsonUtil.object2json(map));

                        }
                    }
                }
            }
        }

        return result;
    }

    //region javadoc注释 : get1
    /**
     * 查询登录用户管辖范围下的的调压设备 <br/><br/>
     * 简介 : 查询登录用户管辖范围下的的调压设备 <br/>
     * 步骤 : <br/>
     * 1. 查询调压设备 : <br/>
     * 根据条件参数查询采集设备(COLLECT_DEVICE)对象集合collectDeviceList_1， <br/>
     * 额外筛选规则如下： <br/>
     *           (1) collectDeviceList_1[i].resourceCode like 登录用户.userCode + '%'; <br/>
     *           (2) collectDeviceList_1[i].categoryCode = REGULATOR_BOX or USER_REGULATOR_TANK or SOURCE_REGULATOR_TANK； <br/>
     * 2. 返回信息 : <br/>
     * 根据collectDeviceList_1及条件参数生成结果对象result并返回， <br/>
     * 其中，result.modelList[i]或result.model的managementName、subCompanyName属性的设置规则如下： <br/>
     *           根据result.modelList[i]或result.model的deptId属性以GLOBAL方式调用“查询部门”服务(user-service: /depts GET)， <br/>
     *           获取到对应部门模型的managementName、subCompanyName属性进行设置； <br/>
     * RegulatorDeviceModel.qrCode的设置规则如下： <br/>
     *           RegulatorDeviceModel.qrCode = {'code' : RegulatorDeviceModel.code, 'categoryCode' : RegulatorDeviceModel.categoryCode} <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_managementName : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示管理站名称;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_subCompanyName : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示分公司名称;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _is_paging : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否分页;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where COLLECT_DEVICE.ID = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where COLLECT_DEVICE.CODE = #{_equal_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where COLLECT_DEVICE.CODE like #{_like_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_categoryCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where COLLECT_DEVICE.CATEGORY_CODE like #{_equal_categoryCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_deptId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where COLLECT_DEVICE.DEPT_ID = #{_equal_deptId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _like_installAddress : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where COLLECT_DEVICE.INSTALL_ADDRESS like % + #{_like_installAddress} + %;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @throws ServiceException <br/>
     * @see RegulatorDeviceGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get1
    //查询登录用户管辖范围下的的调压设备
    //简介 : 查询登录用户管辖范围下的的调压设备
    //步骤 :
    //1. 查询调压设备 :
    //根据条件参数查询采集设备(COLLECT_DEVICE)对象集合collectDeviceList_1，
    //额外筛选规则如下：
    //          (1) collectDeviceList_1[i].resourceCode like 登录用户.userCode + '%';
    //          (2) collectDeviceList_1[i].categoryCode = REGULATOR_BOX or USER_REGULATOR_TANK or SOURCE_REGULATOR_TANK；
    //2. 返回信息 :
    //根据collectDeviceList_1及条件参数生成结果对象result并返回，
    //其中，result.modelList[i]或result.model的managementName、subCompanyName属性的设置规则如下：
    //          根据result.modelList[i]或result.model的deptId属性以GLOBAL方式调用“查询部门”服务(user-service: /depts GET)，
    //          获取到对应部门模型的managementName、subCompanyName属性进行设置；
    //RegulatorDeviceModel.qrCode的设置规则如下：
    //          RegulatorDeviceModel.qrCode = {'code' : RegulatorDeviceModel.code, 'categoryCode' : RegulatorDeviceModel.categoryCode}
    //endregion
    //region 参数 : get1
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_managementName", value = "是否展示管理站名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_subCompanyName", value = "是否展示分公司名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where RegulatorDeviceModel.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_code", value = "筛选条件 : where RegulatorDeviceModel.code = #{equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_code", value = "筛选条件 : where RegulatorDeviceModel.code like #{like_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_categoryCode", value = "筛选条件 : where RegulatorDeviceModel.categoryCode = #{equal_categoryCode}",required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_deptId", value = "筛选条件 : where RegulatorDeviceModel.deptId = #{equal_deptId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "like_installAddress", value = "筛选条件 : where RegulatorDeviceModel.installAddress like % + #{like_installAddress} + %",required = false, dataType = "string", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_managementName", value = "是否展示管理站名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_subCompanyName", value = "是否展示分公司名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_is_paging", value = "是否分页", defaultValue = "true",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where COLLECT_DEVICE.ID = #{_equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_code", value = "筛选条件 : where COLLECT_DEVICE.CODE = #{_equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_code", value = "筛选条件 : where COLLECT_DEVICE.CODE like #{_like_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_categoryCode", value = "筛选条件 : where COLLECT_DEVICE.CATEGORY_CODE like #{_equal_categoryCode}",required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_deptId", value = "筛选条件 : where COLLECT_DEVICE.DEPT_ID = #{_equal_deptId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_like_installAddress", value = "筛选条件 : where COLLECT_DEVICE.INSTALL_ADDRESS like % + #{_like_installAddress} + %",required = false, dataType = "string", paramType = "query"),

    })
    //endregion
    //region 请求 : get1
    @RequestMapping(value = "/v1/regulator-devices", method = RequestMethod.GET)
    //endregion
    public RegulatorDeviceGetModel get1(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        //1. 查询调压设备 :
        // 根据条件参数查询采集设备(COLLECT_DEVICE)对象集合collectDeviceList_1，
        // 额外筛选规则如下：
        // (1) collectDeviceList_1[i].manageUserId = 登录用户.userId;
        // (2) collectDeviceList_1[i].categoryCode = REGULATOR_BOX or USER_REGULATOR_TANK or SOURCE_REGULATOR_TANK；

        //生成查询模型
        UserCardModel userCard = getUserCard(header);
        CollectDeviceCustomQueryModel queryModel = collectDeviceCustomConvertor.collectDeviceService_query2queryModel(query);
        queryModel.setLike_resourceCode(userCard.getUserCode() + "%");
        queryModel.setEqual_isValid("1");
        String categoryCode1 = deviceOptions.getCode(DeviceOptionsTypeName.COLLECT_DEVICE_CATEGORY, DeviceOptionsName.COLLECT_DEVICE_CATEGORY.REGULATOR_BOX);
        String categoryCode2 = deviceOptions.getCode(DeviceOptionsTypeName.COLLECT_DEVICE_CATEGORY, DeviceOptionsName.COLLECT_DEVICE_CATEGORY.USER_REGULATOR_TANK);
        String categoryCode3 = deviceOptions.getCode(DeviceOptionsTypeName.COLLECT_DEVICE_CATEGORY, DeviceOptionsName.COLLECT_DEVICE_CATEGORY.SOURCE_REGULATOR_TANK);
        String _like_installAddress = HzhrTypeUtil.object2string(query.get("_like_installAddress"));
        if (!StringUtils.isEmpty(_like_installAddress)) {
            queryModel.setLike_installAddress("%" + _like_installAddress + "%");
        }
        queryModel.setCustom1(categoryCode1);
        queryModel.setCustom2(categoryCode2);
        queryModel.setCustom3(categoryCode3);
        //查询实体
        List<CollectDeviceEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = collectDeviceDao.selectExtList2(queryModel);
        }
        //查询数量
        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = collectDeviceDao.selectCount2(queryModel);
        }

        //2. 返回信息
        RegulatorDeviceGetModel result = collectDeviceCustomConvertor.extList2regulatorDeviceGetModel(extList, total, null, query);
        RegulatorDeviceModel model = result.getModel();
        if (model != null) {
            Map<String, Object> query1 = new HashMap<>();
            query1.put("show", "one");
            query1.put("_equal_id", model.getDeptId());
            Boolean _show_managementName = HzhrTypeUtil.object2boolean(query.get("_show_managementName"));
            if (_show_managementName != null && _show_managementName) {
                query1.put("_show_managementName", true);
            }
            Boolean _show_subCompanyName = HzhrTypeUtil.object2boolean(query.get("_show_subCompanyName"));
            if (_show_subCompanyName != null && _show_subCompanyName) {
                query1.put("_show_subCompanyName", true);
            }
            DeptGetModel deptGetModel = user_deptService.get(initHeader(header, AccessModeEnum.GLOBAL), query1);
            DeptModel dept = deptGetModel.getModel();
            if (dept != null) {
                model.setManagementName(dept.getManagementName());
                model.setSubCompanyName(dept.getSubCompanyName());
                Map<String, Object> map = new HashMap<>();
                String code = model.getCode();
                if (code != null){
                    map.put("code", code);
                }
                String categoryCode = model.getCategoryCode();
                if (categoryCode != null){
                    map.put("categoryCode", categoryCode);
                }

                model.setQrCode(HzhrJsonUtil.object2json(map));
            }
        }
        List<RegulatorDeviceModel> modelList = result.getModelList(); //modeList
        if (modelList != null) {
            List<Integer> deptIds = new ArrayList<>();
            for (RegulatorDeviceModel model1 :
                    modelList) {
                if (model1.getDeptId() != null) {
                    deptIds.add(model1.getDeptId());
                }
            }

            Map<String, Object> query1 = new HashMap<>();
            query1.put("show", "list");
            query1.put("_in_id", deptIds);
            //is_paging
            query1.put("_is_paging", false);

            Boolean _show_managementName = HzhrTypeUtil.object2boolean(query.get("_show_managementName"));
            if (_show_managementName != null && _show_managementName) {
                query1.put("_show_managementName", true);
            }
            Boolean _show_subCompanyName = HzhrTypeUtil.object2boolean(query.get("_show_subCompanyName"));
            if (_show_subCompanyName != null && _show_subCompanyName) {
                query1.put("_show_subCompanyName", true);
            }

            DeptGetModel deptGetModel = user_deptService.get(initHeader(header, AccessModeEnum.GLOBAL), query1);
            List<DeptModel> depts = deptGetModel.getModelList();

            for (RegulatorDeviceModel model2:
                    modelList) {
                Integer deptId  = model2.getDeptId();
                if (deptId != null) {
                    for (DeptModel deptModel :
                            depts) {
                        if (deptId.equals(deptModel.getId())) {
                            model2.setManagementName(deptModel.getManagementName());
                            model2.setSubCompanyName(deptModel.getSubCompanyName());

                            Map<String, Object> map = new HashMap<>();
                            String code = model2.getCode();
                            if (code != null){
                                map.put("code", code);
                            }
                            String categoryCode = model2.getCategoryCode();
                            if (categoryCode != null) {
                                map.put("categoryCode", categoryCode);
                            }
                            model2.setQrCode(HzhrJsonUtil.object2json(map));

                        }
                    }
                }
            }
        }

        return result;
    }

}

